package com.yonyou.pmclouds.messagenotice.constructor.impl;

import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.entity.Entity;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.copyperson.entity.CopyPersonVO;
import com.yonyou.pmclouds.messagenotice.entity.MessageNoticeVO;
import com.yonyou.pmclouds.problemlog.entity.ProblemLogBillStatus;
import com.yonyou.pmclouds.problemlog.entity.ProblemLogVO;
import com.yonyou.pmclouds.project.entity.ProjectVO;
import com.yonyou.pmclouds.project.rmiitf.ProjectSimpleQuery;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class ProblemLogNoticeConstructor extends AbstractMessageNoticeConstructor {

    private final ProjectSimpleQuery projectSimpleQuery;

    @Autowired
    public ProblemLogNoticeConstructor(ProjectSimpleQuery projectSimpleQuery) {
        this.projectSimpleQuery = projectSimpleQuery;
    }


    @Override
    protected MessageNoticeVO[] generateContent(Entity vo) throws BusinessException {
        // 您提交的的整改问题已审核通过
        if(vo == null || !(vo instanceof ProblemLogVO)){
            return null;
        }

        ProblemLogVO problemLogVO = (ProblemLogVO)vo;

        ProjectVO projectVO = projectSimpleQuery.queryProject(problemLogVO.getPk_project());

        List<MessageNoticeVO> noticeVOList = new ArrayList<>();



        String messageContent = problemLogVO.getCreator_name() + "提交的[问题记录]";

        // 验收通过给整改人发送通知
        if(ProblemLogVO.OPERATION_ADD.equalsIgnoreCase((problemLogVO.getOperation()))){
            // 制单时给抄送人发送通知：xxx提交的【问题记录】抄送了您
            messageContent = messageContent + "抄送了您";
        }else if(ProblemLogVO.OPERATION_ASSIGN.equalsIgnoreCase(problemLogVO.getOperation())){
            // 交办提交时给抄送人和制单人发送通知：xxx提交的【问题记录】交办给xxx
            messageContent = messageContent + "交办给" + problemLogVO.getRectificationer_name();
            // 交办给制单人发送通知
            noticeVOList.add(getNoticeVO(problemLogVO,projectVO.getSname(), messageContent, problemLogVO.getCreator()));
        }else if(ProblemLogVO.OPERATION_REPAIR.equalsIgnoreCase(problemLogVO.getOperation())){
            // 整改完给抄送人发送通知：xxx提交的【问题记录】已整改
            messageContent = messageContent + "已整改";
        }else if(ProblemLogVO.OPERATION_PASS.equalsIgnoreCase(problemLogVO.getOperation())){
            messageContent = messageContent + "已关闭";
        }else if(ProblemLogVO.OPERATION_NO_PASS.equalsIgnoreCase(problemLogVO.getOperation())){
            messageContent = messageContent + "验收未通过";
        }

        // 给抄送人发送通知
        List<CopyPersonVO> copyPersonVOS = problemLogVO.getCopyPersonVOS();
        if(copyPersonVOS != null && copyPersonVOS.size() > 0){
            for(CopyPersonVO copyPersonVO : copyPersonVOS){
                noticeVOList.add(getNoticeVO(problemLogVO,projectVO.getSname(), messageContent, copyPersonVO.getCopyPerson()));
            }
        }

        // 验收通过给整改人发送通知
        if(problemLogVO.getBillstatus() == ProblemLogBillStatus.STATUS_END){
            messageContent = APPROVAL_PREFIX_MESSAGE + "整改问题已审核通过";
            noticeVOList.add(getNoticeVO(problemLogVO,projectVO.getSname(), messageContent, problemLogVO.getPk_rectificationer()));

        }

        return noticeVOList.toArray(new MessageNoticeVO[]{});
    }

    private MessageNoticeVO getNoticeVO(ProblemLogVO problemLogVO, String title,String content, String receiver){
        MessageNoticeVO noticeVO = new MessageNoticeVO();
        noticeVO.setRelateType(problemLogVO.getBill_type());
        noticeVO.setRelateKey(problemLogVO.getPk_problem());
        noticeVO.setMessageTitle(title);
        noticeVO.setMessageReceiver(receiver);
        noticeVO.setPkTenant(problemLogVO.getPk_tenant());
        noticeVO.setMessageContent(content);
        return noticeVO;
    }

    @Override
    public String getRelateType() {
        return BillTypeConst.PROBLEMLOG_BILLTYPE;
    }
}
